/*
 * @lc app=leetcode.cn id=27 lang=javascript
 *
 * [27] 移除元素
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @param {number} val
 * @return {number}
 */
var removeElement = function (nums, val) {
  const n = nums.length;
  let end = n - 1,
    i = 0;
  let cnt = 0;
  while (i <= end) {
    if (nums[i] === val) {
      cnt++;
      nums[i] = nums[end];
      end--;
    } else {
      i++;
    }
  }
  return n - cnt;
};
// @lc code=end
